<?php

require_once 'Connection.php';
require_once '../Model/DAO/Player.php';
require_once '../Model/DAO/PlayerBalance.php';
require_once '../Model/DAO/PlayerInformation.php';
require_once '../Model/DAO/Betthreads.php';
require_once '../Model/DAL/Functions.php';

class AgentManagement {

    private $con;

    function __construct() {
        $conn = new Connection();
        $this->con = $conn->configDB();
    }

    function createUser() {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $rePassword = $_POST['repassword'];
        $email = $_POST['email'];
        $firstName = $_POST['firstname'];
        $lastName = $_POST['lastname'];
        $gender = $_POST['gender'];
        $address = $_POST['address'];
        $phone = $_POST['phone'];
        $levelID = $_POST['level'];
        $parentID = $_POST['parentid'];
        $role = $_POST['role'];
        $date = date('d-m-Y H:i:s');

        $func = new Functions();
        $salt = $func->generateString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890", 8);
        $saltEnc = base64_encode($salt);
        $passEnc = md5($password . $saltEnc) . $salt;

        $pl = new Player();
        $pl->setUsername($username);
        $pl->setCreateDate($date);
        $pl->setEmail($email);
        $pl->setIsDelete('N');
        $pl->setIsLocked('N');
        $pl->setLevelID($levelID);
        $pl->setMaxMember(0);
        $pl->setParentID($parentID);
        $pl->setPassword($passEnc);
        $pl->setRole($role);
        $pl->setSalt($saltEnc);
        $playerID = $pl->insertPlayer($this->con);

        $pli = new PlayerInformation();
        $pli->setPlayerID($playerID);
        $pli->setAddress($address);
        $pli->setFirstname($firstName);
        $pli->setGender($gender);
        $pli->setLastname($lastName);
        $pli->setPhone($phone);
        $pli->insertPlayerInformation($this->con);

        $plb = new PlayerBalance();
        $plb->setPlayerID($playerID);
        $plb->setBMoney('200000');
        $plb->insertPlayerBalance($this->con);

        return $plb;
    }

    function getAllPlayer() {
        $parentID = isset($_GET["parentid"]) ? $_GET["parentid"] : $_SESSION['agentLog']['PlayerID'];
        $limit = 15;
        $range = 10;
        $start = (isset($_GET['page'])) ? ($_GET['page']) : 1;
        $player = new Player();
        $player->setParentID($parentID);
        $result = $player->getAllPlayer($this->con, $start, $limit);

        $arr = array();
        $arr["Data"] = $result["Data"];
        $total = isset($_GET["total"]) ? $_GET["total"] : $result["Total"];

        $arr["Start"] = $start;
        $arr["Total"] = $total;
        $arr["Limit"] = $limit;
        $arr["Range"] = $range;
        return $arr;
    }

    function getAllPlayerBalance() {
        $parentID = isset($_GET["parentid"]) ? $_GET["parentid"] : $_SESSION['agentLog']['PlayerID'];
        $limit = 15;
        $range = 10;
        $start = (isset($_GET['page'])) ? ($_GET['page']) : 1;
        $player = new Player();
        $player->setParentID($parentID);
        $result = $player->getAllPlayerBalance($this->con, $start, $limit);

        $arr = array();
        $arr["Data"] = $result["Data"];
        $total = isset($_GET["total"]) ? $_GET["total"] : $result["Total"];

        $arr["Start"] = $start;
        $arr["Total"] = $total;
        $arr["Limit"] = $limit;
        $arr["Range"] = $range;
        return $arr;
    }

    function getAllSubPlayer() {
        $parentID = $_GET['parentid'];
        $player = new Player();
        $arr = array();
        $player->setParentID($parentID);
        $arr = $player->getAllSubPlayer($this->con);
        return $arr;
    }

    function getAccountBalance() {
        $agentLog = $_SESSION['agentLog'];
        $playerID = $agentLog['PlayerID'];
        $player = new Player();
        $player->setPlayerID($playerID);
        return $player->getAccountbalance($this->con);
    }

    function changepassword() {
        $oldPassword = $_POST['oldpassword'];
        $newPassword = $_POST['newpassword'];
        $playerid = $_POST['playerid'];
        $player = new Player();
        $player->setPlayerID($playerid);
        $player->setPassword($newPassword);
        $player->setOldpassword($oldPassword);
        return $player->changPassword($this->con);
    }

    function loadAccountDetail() {
        $playerID = $_GET['pl'];
        $player = new Player();
        $player->setPlayerID($playerID);
        return $player->loadAccountDetail($this->con);
    }

    function updatePlayer() {
        $playerId = $_POST['playerid'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $islocked = $_POST['islocked'];
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $email = $_POST['email'];
        $gender = $_POST['gender'];

        $player = new Player();
        $player->setPlayerID($playerId);
        $player->setUsername($username);
        $player->setPassword($password);
        $player->setIsLocked($islocked);
        $player->setEmail($email);
        $playerInfor = new PlayerInformation();
        $playerInfor->setGender($gender);
        $playerInfor->setFirstname($firstname);
        $playerInfor->setLastname($lastname);
        $player->setPlayerInformation($playerInfor);
        return $player->updatePlayer($this->con);
    }

    function getMaxDateTime() {
        $player = new Player();
        $arrPlayer = array();
        $arrPlayer = $player->getMaxDateTime($this->con);
        return $arrPlayer;
    }

    function getTopUserWin() {
        $limit = 10;
        $range = 10;
        $start = (isset($_GET['page'])) ? ($_GET['page']) : 1;
        $start = ($start == 0) ? 1 : $start;
        $player = new Player();
        $maxdate = $this->getMaxDateTime();
        $originalDate = $maxdate['MaxDate'];
        $newDate = date("d-m-Y", strtotime($originalDate));
        $betthreads = new betthreads();
        $betthreads->setBetDate($newDate);

        $player->setBetthreads($betthreads);
        $result = $player->getTopUserWin($this->con, $start, $limit);

        $arr["Data"] = $result["Data"];
        $total = isset($_GET["total"]) ? $_GET["total"] : $result["Total"];

        $arr["Start"] = $start;
        $arr["Total"] = $total;
        $arr["Limit"] = $limit;
        $arr["Range"] = $range;
        return $arr;
    }

    function getTopUserLose() {
        $limit = 10;
        $range = 10;
        $start = (isset($_GET['page'])) ? ($_GET['page']) : 1;
        $start = ($start == 0) ? 1 : $start;
        $player = new Player();
        $maxdate = $this->getMaxDateTime();
        $originalDate = $maxdate['MaxDate'];
        $newDate = date("d-m-Y", strtotime($originalDate));
        $betthreads = new Betthreads();
        $betthreads->setBetDate($newDate);
        $player->setBetthreads($betthreads);
        $result = $player->getTopUserLose($this->con, $start, $limit);

        $arr["Data"] = $result["Data"];
        $total = isset($_GET["total"]) ? $_GET["total"] : $result["Total"];

        $arr["Start"] = $start;
        $arr["Total"] = $total;
        $arr["Limit"] = $limit;
        $arr["Range"] = $range;
        return $arr;
    }

    function getAllReportOfPlayer() {
        $limit = 15;
        $range = 10;
        $start = (isset($_GET['page'])) ? ($_GET['page']) : 0;
        $start = ($start == 0) ? 1 : $start;
        $agentLog = $_SESSION['agentLog'];
        $playerID = $agentLog['PlayerID'];
        $role = $agentLog['Role'];
        $player = new Player();
        $player->setPlayerID($playerID);
        $player->setRole($role);
        $result = $player->getAllReportOfPlayer($this->con, $start, $limit);

        $arr["Data"] = $result["Data"];
        $total = isset($_GET["total"]) ? $_GET["total"] : $result["Total"];
        $resultExport = $player->getAllReportOfPlayer($this->con, 1, $total);
        $arr["DataExport"] = $resultExport["Data"];
        $_SESSION["arrDataExport"] = $arr["DataExport"];

        $arr["Start"] = $start;
        $arr["Total"] = $total;
        $arr["Limit"] = $limit;
        $arr["Range"] = $range;
        return $arr;
    }

    function showHistoryReport() {
        $limit = 15;
        $range = 10;
        $start = (isset($_GET['page'])) ? ($_GET['page']) : 1;
        $start = ($start == 0) ? 1 : $start;
        $agentLog = $_SESSION['agentLog'];
        $playerID = $agentLog['PlayerID'];
        $role = $agentLog['Role'];
        $player = new Player();
        $player->setPlayerID($playerID);
        $player->setRole($role);
        $result = $player->showHistoryReport($this->con, $start, $limit);

        $arr["Data"] = $result["Data"];
        $total = isset($_GET["total"]) ? $_GET["total"] : $result["Total"];
        $resultExport = $player->getAllReportOfPlayer($this->con, 0, $total);
        $arr["DataExport"] = $resultExport["Data"];
        $_SESSION["arrDataExport"] = $arr["DataExport"];

        $arr["Start"] = $start;
        $arr["Total"] = $total;
        $arr["Limit"] = $limit;
        $arr["Range"] = $range;
        return $arr;
    }

    function getChartRevenusDayByDay() {
        $day = $_POST['day'];
        session_start();
        $agentLog = $_SESSION['agentLog'];
        $playerID = $agentLog['PlayerID'];
        $role = $agentLog['Role'];
        $player = new Player();
        $player->setPlayerID($playerID);
        $player->setRole($role);
        $arrPlayer = array();
        $arrPlayer = $player->getRevenusDayByDay($this->con, $day);
        return $arrPlayer;
    }

    function getChartRevenusDayByNoDay() {
        $noDay = isset($_GET["noDay"]) ? $_GET["noDay"] : 0;
        $agentLog = $_SESSION['agentLog'];
        $playerID = $agentLog['PlayerID'];
        $role = $agentLog['Role'];
        $player = new Player();
        $player->setPlayerID($playerID);
        $player->setRole($role);
        $result = $player->getRevenusDayByNoDay($this->con, $noDay);
        return $result;
    }

    function getChartRevenusYearWithMonthByYear() {
        $year = $_POST['year'];
        session_start();
        $agentLog = $_SESSION['agentLog'];
        $playerID = $agentLog['PlayerID'];
        $role = $agentLog['Role'];
        $player = new Player();
        $player->setPlayerID($playerID);
        $player->setRole($role);
        $arrPlayer = array();
        $arrPlayer = $player->getRevenusYearWithMonthByYear($this->con, $year);
        return $arrPlayer;
    }

    function getChartRevenusYearWithMonthByNoYear() {
        $noYear = isset($_GET["noYear"]) ? $_GET["noYear"] : 0;
        $agentLog = $_SESSION['agentLog'];
        $playerID = $agentLog['PlayerID'];
        $role = $agentLog['Role'];
        $player = new Player();
        $player->setPlayerID($playerID);
        $player->setRole($role);
        $result = $player->getRevenusYearWithMonthByNoYear($this->con, $noYear);
        return $result;
    }

    function getChartRevenusWeekByNoDay() {
        $noWeek = isset($_GET["noWeek"]) ? $_GET["noWeek"] : 1;
        $agentLog = $_SESSION['agentLog'];
        $playerID = $agentLog['PlayerID'];
        $role = $agentLog['Role'];
        $player = new Player();
        $player->setPlayerID($playerID);
        $player->setRole($role);
        $result = $player->getRevenusWeekByNoDay($this->con, $noWeek);
        return $result;
    }

}
